/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$        : main.c                                        	      *
 *  $Author$           : ChipON AE/FAE Group                                  *
 *  $Data$             : 2022-3-17                                           *
 *  $Software Version  : V1.5	                                              *
 *  $Description$     : This is a KF32A156 device StdPeriph Template		  *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyrght protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 ******************************************************************************

/******************************************************************************
*                        		Include Files                                 *
******************************************************************************/
#include "system_init.h"
#include "Canfd.h"
static void Led_Gpio_Init(void);
static void Led_Flip(void);
uint32_t Receive_Flag = 0x00;
Canfd_MailboxHeaderType  	Receive_Canfd_MailboxHeader = {0};

static void Led_Gpio_Init(void)
{
/* 
CCP0CH1    PD12
CCP21CH3   PH3
ECCP5CH3H  PA4
EPWM13A    PF7
 */

	GPIO_Write_Mode_Bits(GPIOA_SFR, GPIO_PIN_MASK_4 , GPIO_MODE_OUT);
	GPIO_Write_Mode_Bits(GPIOD_SFR, GPIO_PIN_MASK_12 , GPIO_MODE_OUT);
	GPIO_Write_Mode_Bits(GPIOF_SFR, GPIO_PIN_MASK_7 , GPIO_MODE_OUT);
	GPIO_Write_Mode_Bits(GPIOH_SFR, GPIO_PIN_MASK_3 , GPIO_MODE_OUT);
	GPIO_Write_Mode_Bits(GPIOA_SFR, GPIO_PIN_MASK_2 , GPIO_MODE_OUT);

}


/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
static void Led_Flip(void)
{
	GPIO_Toggle_Output_Data_Config(GPIOA_SFR, GPIO_PIN_MASK_4);
	GPIO_Toggle_Output_Data_Config(GPIOD_SFR, GPIO_PIN_MASK_12);
	GPIO_Toggle_Output_Data_Config(GPIOF_SFR, GPIO_PIN_MASK_7);
	GPIO_Toggle_Output_Data_Config(GPIOH_SFR, GPIO_PIN_MASK_3);
}


/*
 * 500K  BAUDRATE
 * */
int main()
{
	/* Initialize the system clock */
	SystemInit(120);
	/* Setup SysTick Timer as delay function */
	systick_delay_init(120);
	Canfd_Gpio_Init();
	Led_Gpio_Init();
	Can_m_FdControllerDeInit(CANfd7);
	Can_m_FdControllerInit(CANfd7, Canfd_Controller_AllConfig,Initindex_0);

	/* Initialize Receveive Mailbox */
	Receive_Canfd_MailboxHeader.TransceiveType = MAIL_RECEIVE;
	Can_m_FdMailBox_Write(CANfd7,Mailbox_0,&Receive_Canfd_MailboxHeader);

	while (1)
	{
		/* Initialize Transmit Mailbox */
		if(Can_m_FdMailBox_Write(CANfd7,Mailbox_1,&Canfd_Sdu.Can_MailboxHeader[0]) == CAN_OK)
		{
			/* Enable Transmit */
			(void)Can_m_FdTransmitonce(CANfd7);
		}

		if(Receive_Flag > 0x00)
		{
			Receive_Canfd_MailboxHeader.Id = 0x110;
			Receive_Canfd_MailboxHeader.TransceiveType = MAIL_TRANSMIT;
			if(Can_m_FdMailBox_Write(CANfd7,Mailbox_2,&Receive_Canfd_MailboxHeader) == CAN_OK)
			{
				/* Enable Transmit */
				(void)Can_m_FdTransmitonce(CANfd7);
				Receive_Flag--;
			}
		}
		systick_delay_ms(250);
		Led_Flip();
	}
}



